home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / eulisp / you-075a.lha / you-075a / yyfudge.c < prev   
C/C++ Source or Header  |  1992-06-18  |  6KB  |  264 lines

  1. /* swap streams in flex */
  2. /* $Id: yyfudge.c,v 1.3 1991/11/15 13:45:56 pab Exp $ */
  3. /* $Log: yyfudge.c,v $
  4.  * Revision 1.3  1991/11/15  13:45:56  pab
  5.  * copyalloc rev 0.01
  6.  *
  7.  * Revision 1.2  1991/09/11  12:07:55  pab
  8.  * 11/9/91 First Alpha release of modified system
  9.  *
  10.  * Revision 1.1  1991/08/12  16:50:17  pab
  11.  * Initial revision
  12.  *
  13.  * Revision 1.7  1991/05/15  09:18:14  pab
  14.  * 'C' garbage collector changes
  15.  *
  16.  * Revision 1.6  1991/02/14  01:40:09  kjp
  17.  * Added a modified version of yy_switch_to_buffer to make multi-file
  18.  * reading work "correctly".
  19.  *
  20.  * Revision 1.5  1991/02/11  17:59:50  is
  21.  * Added support for read-with-line-numbers
  22.  *
  23. */
  24.  
  25. static SYSTEM_GLOBAL_ARRAY1(YY_BUFFER_STATE,yy_savebuf_list,32);
  26. static SYSTEM_GLOBAL_ARRAY1(int,yy_save_lineno,32);
  27.  
  28. #ifdef YY_USE_PROTOS
  29. void my_yy_switch_to_buffer( YY_BUFFER_STATE new_buffer )
  30. #else
  31. void my_yy_switch_to_buffer( new_buffer )
  32. YY_BUFFER_STATE new_buffer;
  33. #endif
  34.  
  35.     {
  36.     
  37.     /* This line must be ignored for the buffer swapping to work */
  38.  
  39.     /*
  40.     if ( yy_current_buffer == new_buffer )
  41.     return;
  42.     */
  43.  
  44.     if ( yy_current_buffer )
  45.     {
  46.     /* flush out information for old buffer */
  47.     *yy_c_buf_p = yy_hold_char;
  48.     yy_current_buffer->yy_buf_pos = yy_c_buf_p;
  49.     yy_current_buffer->yy_n_chars = yy_n_chars;
  50.     }
  51.  
  52.     yy_current_buffer = new_buffer;
  53.     yy_load_buffer_state();
  54.  
  55.     /* we don't actually know whether we did this switch during
  56.      * EOF (yywrap()) processing, but the only time this flag
  57.      * is looked at is after yywrap() is called, so it's safe
  58.      * to go ahead and always set it.
  59.      */
  60.     yy_did_buffer_switch_on_eof = 1;
  61.   }
  62.  
  63. #ifdef __STDC__
  64. void my_yyrestart( FILE *input_file )
  65. #else
  66. void my_yyrestart( input_file )
  67. FILE *input_file;
  68. #endif
  69.  
  70. {
  71.     yyin = input_file;
  72.     yy_init = 1;
  73.   }
  74.  
  75. static FILE *yy_last_read_stream = (FILE *) -1;
  76.  
  77. #ifdef __STDC__
  78. void yy_save_stream(FILE *f)
  79. #else
  80. void yy_save_stream(f)
  81. FILE *f;
  82. #endif
  83. {
  84.   int fn;
  85.   extern int lex_input_line_number;
  86.  
  87.   fn = fileno(f);
  88.   SYSTEM_GLOBAL_ARRAY1_VALUE(yy_savebuf_list,fn)= YY_CURRENT_BUFFER;
  89.   SYSTEM_GLOBAL_ARRAY1_VALUE(yy_save_lineno,fn)=lex_input_line_number;
  90. }
  91.  
  92. #define FUDGEBUG(x) /* x;fflush(stdout); */
  93.  
  94.  
  95. #ifdef YY_USE_PROTOS
  96. static void my_copy(char *a,char *b,int n)
  97. #else
  98. static void my_copy(a,b,n)
  99. char *a,*b;
  100. int n;
  101. #endif
  102. {
  103.   for (; n>0; --n) a[n-1] = b[n-1];
  104. }
  105.  
  106. #ifdef YY_USE_PROTOS
  107. YY_BUFFER_STATE my_yy_create_buffer( FILE *file, int size )
  108. #else
  109. YY_BUFFER_STATE my_yy_create_buffer( file, size )
  110. FILE *file;
  111. int size;
  112. #endif
  113. {
  114.   YY_BUFFER_STATE b;
  115.  
  116. #ifdef CGC
  117.   b = (YY_BUFFER_STATE) malloc( sizeof( struct yy_buffer_state ) );
  118. #else 
  119.   b = (YY_BUFFER_STATE) allocate_stack(0, sizeof( struct yy_buffer_state ) );
  120. #endif
  121.   if ( ! b )
  122.     YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );
  123.  
  124.   b->yy_buf_size = size;
  125.  
  126.   /* yy_ch_buf has to be 2 characters longer than the size given because
  127.    * we need to put in 2 end-of-buffer characters.
  128.    */
  129. #ifdef CGC
  130.   b->yy_ch_buf = (YY_CHAR *) malloc( (unsigned) (b->yy_buf_size + 2) );
  131. #else
  132.   /* add 2 to align */
  133.   b->yy_ch_buf = (YY_CHAR *) allocate_stack(0, (unsigned) (b->yy_buf_size + 2 + 2) );
  134. #endif
  135.  
  136.   if ( ! b->yy_ch_buf )
  137.     YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );
  138.  
  139.   yy_init_buffer( b, file );
  140.   
  141.   return ( b );
  142. }
  143.  
  144.  
  145.  
  146. #ifdef YY_USE_PROTOS
  147. void my_yy_delete_buffer( YY_BUFFER_STATE b )
  148. #else
  149. void my_yy_delete_buffer( b )
  150. YY_BUFFER_STATE b;
  151. #endif
  152.  
  153.     {
  154.     if ( b == yy_current_buffer )
  155.     yy_current_buffer = (YY_BUFFER_STATE) 0;
  156. #ifndef CGC
  157.     deallocate_stack(NULL, (char *) b->yy_ch_buf, b->yy_buf_size + 2 + 2);
  158.     deallocate_stack(NULL, (char *) b, sizeof( struct yy_buffer_state ) );
  159. #endif
  160.     }
  161.  
  162. #ifdef __STDC__
  163. void yy_set_stream(FILE *f)
  164. #else
  165. void yy_set_stream(f)
  166. FILE *f;
  167. #endif
  168. {
  169.   YY_BUFFER_STATE sin;
  170.   int fn;
  171.  
  172.   fn = fileno(f);
  173.  
  174.   FUDGEBUG(printf("[Setting to %d]\n",fn));
  175.  
  176.                 /* if (f == yy_last_read_stream) return; */
  177.  
  178.   if (yy_last_read_stream != (FILE *) -1)
  179.     yy_save_stream(yy_last_read_stream);
  180.   yy_last_read_stream = f;
  181.  
  182.   if (SYSTEM_GLOBAL_ARRAY1_VALUE(yy_savebuf_list,fn)!= 0) {
  183.     my_yy_switch_to_buffer(SYSTEM_GLOBAL_ARRAY1_VALUE(yy_savebuf_list,fn));
  184.     lex_input_line_number = SYSTEM_GLOBAL_ARRAY1_VALUE(yy_save_lineno,fn);
  185.  
  186. FUDGEBUG(printf("[Old %d]\n",SYSTEM_GLOBAL_ARRAY1_VALUE(yy_savebuf_list,fn)));
  187.  
  188.   } else {
  189.     sin = my_yy_create_buffer(f,YY_BUF_SIZE);
  190.     SYSTEM_GLOBAL_ARRAY1_VALUE(yy_savebuf_list,fn)= sin;
  191.     my_yy_switch_to_buffer(sin);
  192.     lex_input_line_number=1;
  193. FUDGEBUG(  printf("[New %d]\n",SYSTEM_GLOBAL_ARRAY1_VALUE(yy_savebuf_list,fn));)
  194.  
  195.   }
  196. }
  197.  
  198. #ifdef __STDC__
  199. int yy_close_stream(FILE *fd)
  200. #else
  201. int yy_close_stream(fd)
  202. FILE *fd;
  203. #endif
  204. {
  205.   int num;
  206.  
  207.   num = fileno(fd);
  208.  
  209.   FUDGEBUG(printf("[Closing %d]\n",num));
  210.  
  211.   if (SYSTEM_GLOBAL_ARRAY1_VALUE(yy_savebuf_list,num)!= NULL) {
  212.  
  213.     FUDGEBUG(printf("[Was %d]\n",SYSTEM_GLOBAL_ARRAY1_VALUE(yy_savebuf_list,num));)
  214.     my_yy_delete_buffer(SYSTEM_GLOBAL_ARRAY1_VALUE(yy_savebuf_list,num));
  215.   }
  216.   SYSTEM_GLOBAL_ARRAY1_VALUE(yy_savebuf_list,num) = 0;
  217.  
  218.   FUDGEBUG(printf("[Zeroed %d]\n",SYSTEM_GLOBAL_ARRAY1_VALUE(yy_savebuf_list,num));)
  219.  
  220.   if (fd == yy_last_read_stream) yy_last_read_stream = (FILE *) -1;
  221.   system_fclose(fd);
  222.  
  223.   return(0);
  224. }
  225.  
  226. #ifdef __STDC__
  227. void yy_reset_stream(FILE *fd)
  228. #else
  229. void yy_reset_stream(fd)
  230. FILE *fd;
  231. #endif
  232. {
  233.  
  234. #ifdef __STDC__
  235.   extern void yy_init_buffer(YY_BUFFER_STATE,FILE *);
  236. #else
  237.   extern void yy_init_buffer();
  238. #endif
  239.  
  240.   yy_set_stream(fd);
  241.   yy_save_stream(fd);
  242.   yyrestart(fd);
  243. /*  yy_save_stream(fd); */
  244. }
  245.  
  246. void initialise_fudge()
  247. {
  248.   SYSTEM_INITIALISE_GLOBAL_ARRAY1(YY_BUFFER_STATE,yy_savebuf_list,32,NULL);
  249.   SYSTEM_INITIALISE_GLOBAL_ARRAY1(int,yy_save_lineno,32,1);
  250. }
  251.  
  252. #ifdef __STDC__
  253. int lexer_getc(FILE *fd)
  254. #else
  255. int lexer_getc(fd)
  256. FILE *fd;
  257. #endif
  258. {
  259.   /* reset stream in here */
  260.   yy_reset_stream(fd);
  261.   return (input());
  262. }
  263.  
  264.